/**
 * 
 */
package com.easidea.tim.service.supply;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.supply.SupDao;
import com.easidea.tim.dao.supply.SupTypeDao;
import com.easidea.tim.entity.supply.Sup;
import com.easidea.tim.entity.supply.SupType;
import com.easidea.tim.util.Page;

/**
 * @author Harrison
 *
 */
public class SupTypeService extends EntityService<SupType,SupTypeDao> {
	
	private SupTypeDao supTypeDao;
	private SupDao supDao;
	
	
	
	public SupDao getSupDao() {
		return supDao;
	}

	public void setSupDao(SupDao supDao) {
		this.supDao = supDao;
	}

	@SuppressWarnings("unchecked")
	public Page getSupplierTypeListByContion(String name, Page page) {
		 Criteria ca = getSupplierTypeListByContion(name);
		 ca.setProjection(Projections.count("name"));
		 Integer totalNum = (Integer)ca.uniqueResult();
		 Criteria ca1 = getSupplierTypeListByContion(name);
		 int startIndex = Page.getStartOfPage(page.getCurrentPageNo(), page.getPageSize());
		 ca1.setFirstResult(startIndex);
		 ca1.setMaxResults(page.getPageSize());
		 List<SupType> ls = (List<SupType>)ca1.list();
		 Page resultPage = new Page(startIndex, totalNum, page.getPageSize(), ls);
		 resultPage.setCurrentPageNo(page.getCurrentPageNo());				
		return resultPage;

	}
	
	private Criteria getSupplierTypeListByContion(String name) {
		Criteria ca = this.createUnDeletedCriteria();
		if(name != null && !name.equals("")) {
			ca.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		}		
		return ca;
	}
	
	
	public SupType getSupplierTypeDById(Long id) {
		SupType type = this.getSupplierTypeById(id);
		return type;
	}
	
	public boolean saveOrUpdateSupplierType(SupType type) {	
		if(type.getId() == -1){
			supTypeDao.getHibernateTemplate().save(type);
		}else{
			supTypeDao.getHibernateTemplate().update(type);
		}		
		return true;
	}
	
	public boolean deleteSupplierType(Long id){		
		List<Sup> list = supDao.GetSupplierByType(id);
		if(list.size()==0){
			supTypeDao.getHibernateTemplate().delete(getSupplierTypeById(id));
			return true;
		}else{
			return false;
		}
	}
	/**
	 * TODO 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SupType> getAllSupplierType() {
		// TODO Auto-generated method stub
		Criteria ca = createCriteria();
		return ca.list();

	}
	
	/**
	 * TODO
	 * @param long1
	 * @return
	 */
	public SupType getSupplierTypeById(Long id) {
		SupType type = (SupType) supTypeDao.getHibernateTemplate().get(SupType.class, id);
		return type;
	}

	

	/* (non-Javadoc)
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public SupTypeDao getEntityDao() {
		return supTypeDao;
	}

	public SupTypeDao getSupTypeDao() {
		return supTypeDao;
	}

	public void setSupTypeDao(SupTypeDao supTypeDao) {
		this.supTypeDao = supTypeDao;
	}

	
}
